import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.io.*;

public class JobScheduler {
	
	class Job implements Comparable<Job>{
		int weight;
		int length;
		float keyValue;
		
		public Job(){}
		
		public int compareTo(Job job)
		{
			if(job.keyValue > this.keyValue)
				return 1;
			else if(job.keyValue < this.keyValue)
				return -1;
			else if(job.weight > this.weight)
				return 1;
			else if(job.weight < this.weight)
				return -1;
			else return 0;
		}
	}

	ArrayList<Job> jobList;
	
	public void ReadJobsData(){
		DataReader reader = new DataReader();
		reader.SetFilename("C:\\tmp\\jobs.txt");

		String str;
		str = reader.GetNextLine();
		jobList = new ArrayList<Job>(Integer.parseInt(str));

		
		while((str = reader.GetNextLine()) != null){
			String [] value = str.split(" ");
			Job job = new Job(); 
			job.weight = Integer.parseInt(value[0]);
			job.length = Integer.parseInt(value[1]);
			job.keyValue = job.weight - job.length;
			jobList.add(job);
		}
	}
	
	
	public void PrintData(){
		for(int i = 0; i < jobList.size(); i++)
		{
			Job job = jobList.get(i);
			System.out.print((i+1) + ": ");
			System.out.print("weight: " + job.weight + "\t" + "length: " + job.length + "\t" + "key: " + job.keyValue + "\n");
		}
		
		System.out.print("\n");
	}
	
	public long ComputeWeightedCompletionTime()
	{
		int curTime = 0;
		long weightedTime = 0;
		for(int i = 0; i < jobList.size(); i++)
		{
			Job job = jobList.get(i);
			curTime = curTime + job.length;
			weightedTime += curTime * job.weight;
		}
		
		return weightedTime;
	}
	
	public void ComputeCompletionTime(){
		Collections.sort(jobList);	
	
		System.out.println("weighted completion time by difference is " + ComputeWeightedCompletionTime());
	}
	
	
	public void ArrangeDataByRatio(){
		for(int i = 0; i < jobList.size(); i++)
		{
			Job job = jobList.get(i);
			float w = job.weight;
			float l = job.length;
			job.keyValue = w / l;
		}
	}
	
	
	public void ComputeCompletionTimeByRatio(){
		ArrangeDataByRatio();
		Collections.sort(jobList, new Comparator<Job>(){
			public int compare(Job job1, Job job2)
			{
				if(job1.keyValue > job2.keyValue)
					return -1;
				else if(job1.keyValue < job2.keyValue)
					return 1;
				else 
					return 0;
			}
		});
		
		System.out.println("weighted completion time by ratio is " + ComputeWeightedCompletionTime());
	}
	

}
